package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.WorkflowApplyRpcService;
import com.hunttown.mes.rpc.domain.WorkflowApplyDTO;
import com.hunttown.mes.rpc.domain.query.WorkflowApplyDTOQuery;
import com.hunttown.mes.service.WorkflowApplyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.WorkflowApplyConverts.convert;

/**
 * created by wangjunfu on 2019-08-01 16:55:13
 */
@Service
public class WorkflowApplyRpcServiceImpl implements WorkflowApplyRpcService {

    private final Logger logger = LoggerFactory.getLogger(WorkflowApplyRpcServiceImpl.class);

    private final WorkflowApplyService workflowApplyService;

    @Autowired
    public WorkflowApplyRpcServiceImpl(WorkflowApplyService workflowApplyService) {
        this.workflowApplyService = workflowApplyService;
    }

    @Override
    public WorkflowApplyDTO insert(WorkflowApplyDTO workflowApplyDTO) {
        try {
            return convert(workflowApplyService.insert(convert(workflowApplyDTO)));
        } catch (Exception e) {
            logger.error("WorkflowApply create failed.", e);
            throw new RuntimeException("WorkflowApply create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(WorkflowApplyDTO workflowApplyDTO) {
        try {
            return workflowApplyService.updateInfoById(convert(workflowApplyDTO));
        } catch (Exception e) {
            logger.error("WorkflowApply update by id failed.", e);
            throw new RuntimeException("WorkflowApply update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(WorkflowApplyDTOQuery workflowApplyDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (workflowApplyDTOQuery.getW_id() != null && workflowApplyDTOQuery.getW_id() > 0) {
                query.put("w_id", workflowApplyDTOQuery.getW_id());
            }

            if (StringUtils.isNotBlank(workflowApplyDTOQuery.getApplyTitle())) {
                query.put("applyTitle", workflowApplyDTOQuery.getApplyTitle());
            }

            if (workflowApplyDTOQuery.getIds() != null && !workflowApplyDTOQuery.getIds().equals("")) {
                query.put("ids", workflowApplyDTOQuery.getIds());
            }

            if (workflowApplyDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", workflowApplyDTOQuery.getStopFlag());
            }

            if (workflowApplyDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", workflowApplyDTOQuery.getDeleteFlag());
            }

            return workflowApplyService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("WorkflowApply update by query failed.", e);
            throw new RuntimeException("WorkflowApply update by query failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowApplyDTO selectById(Integer id) {
        try {
            return convert(workflowApplyService.selectById(id));
        } catch (Exception e) {
            logger.error("WorkflowApply select by id failed.", e);
            throw new RuntimeException("WorkflowApply select by id failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowApplyDTO selectObjByQuery(WorkflowApplyDTOQuery workflowApplyDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (workflowApplyDTOQuery.getTableId() != null && workflowApplyDTOQuery.getTableId() > 0) {
                query.put("tableId", workflowApplyDTOQuery.getTableId());
            }

            if (workflowApplyDTOQuery.getRecordId() != null && workflowApplyDTOQuery.getRecordId() > 0) {
                query.put("recordId", workflowApplyDTOQuery.getRecordId());
            }

            if (workflowApplyDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", workflowApplyDTOQuery.getStopFlag());
            }

            if (workflowApplyDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", workflowApplyDTOQuery.getDeleteFlag());
            }

            return convert(workflowApplyService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("WorkflowApply select obj by query failed.", e);
            throw new RuntimeException("WorkflowApply select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<WorkflowApplyDTO> getListForPage(WorkflowApplyDTOQuery workflowApplyDTOQuery) {
        Query query = Query.page(workflowApplyDTOQuery.getPn(), workflowApplyDTOQuery.getPageSize());

        if (workflowApplyDTOQuery.getId() != null && workflowApplyDTOQuery.getId() > 0) {
            query.put("id", workflowApplyDTOQuery.getId());
        }

        if (workflowApplyDTOQuery.getIds() != null && !workflowApplyDTOQuery.getIds().equals("")) {
            query.put("ids", workflowApplyDTOQuery.getIds());
        }

        if (workflowApplyDTOQuery.getTableId() != null && workflowApplyDTOQuery.getTableId() > 0) {
            query.put("tableId", workflowApplyDTOQuery.getTableId());
        }

        if (workflowApplyDTOQuery.getRecordId() != null && workflowApplyDTOQuery.getRecordId() > 0) {
            query.put("recordId", workflowApplyDTOQuery.getRecordId());
        }

        if (workflowApplyDTOQuery.getBusinessId() != null && workflowApplyDTOQuery.getBusinessId() > 0) {
            query.put("businessId", workflowApplyDTOQuery.getBusinessId());
        }

        if (StringUtils.isNotBlank(workflowApplyDTOQuery.getLikeName())) {
            query.put("likeName", workflowApplyDTOQuery.getLikeName());
        }

        if (workflowApplyDTOQuery.getApplyType() != null) {
            query.put("applyType", workflowApplyDTOQuery.getApplyType());
        }

        if (workflowApplyDTOQuery.getStatus() != null && workflowApplyDTOQuery.getStatus() != -9999) {
            query.put("status", workflowApplyDTOQuery.getStatus());
        }

        if (workflowApplyDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", workflowApplyDTOQuery.getStopFlag());
        }

        if (workflowApplyDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", workflowApplyDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(workflowApplyDTOQuery.getOrderBy())) {
            query.put("orderBy", workflowApplyDTOQuery.getOrderBy());
        }

        //申请日期
        if (StringUtils.isNotBlank(workflowApplyDTOQuery.getDaterange())) {
            String[] date = workflowApplyDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        //不包括取消和撤消、驳回数据（废弃）
        if (workflowApplyDTOQuery.getNoCancle() != null && workflowApplyDTOQuery.getNoCancle() == 1) {
            query.put("noCancle", workflowApplyDTOQuery.getNoCancle());
        }

        List<WorkflowApplyDTO> list = convert(workflowApplyService.selectByQuery(query));
        Page<WorkflowApplyDTO> page = Page.from(workflowApplyDTOQuery.getPn(), workflowApplyDTOQuery.getPageSize(), list);
        page.setTotalItems(workflowApplyService.countByQuery(query));

        return page;
    }

    @Override
    public Page<WorkflowApplyDTO> getJoinSecondaryForPage(WorkflowApplyDTOQuery workflowApplyDTOQuery) {
        Query query = Query.page(workflowApplyDTOQuery.getPn(), workflowApplyDTOQuery.getPageSize());

        if (workflowApplyDTOQuery.getApplyType() != null) {
            query.put("applyApplyType", workflowApplyDTOQuery.getApplyType());
        }

        if (StringUtils.isNotBlank(workflowApplyDTOQuery.getApplyLikeName())) {
            query.put("applyLikeName", workflowApplyDTOQuery.getApplyLikeName());
        }

        if (workflowApplyDTOQuery.getSecondaryCurrUser() != null && workflowApplyDTOQuery.getSecondaryCurrUser() > 0) {
            query.put("secondaryCurrUser", workflowApplyDTOQuery.getSecondaryCurrUser());
        }

        if (workflowApplyDTOQuery.getIsCurrent() != null && workflowApplyDTOQuery.getIsCurrent() != -9999) {
            query.put("isCurrent", workflowApplyDTOQuery.getIsCurrent());
        }

        if (workflowApplyDTOQuery.getNodeSort() != null) {
            query.put("nodeSort", workflowApplyDTOQuery.getNodeSort());
        }

        if (workflowApplyDTOQuery.getLineBusinessId() != null && workflowApplyDTOQuery.getLineBusinessId() > 0) {
            query.put("lineBusinessId", workflowApplyDTOQuery.getLineBusinessId());
        }

        if (workflowApplyDTOQuery.getBusinessId() != null && workflowApplyDTOQuery.getBusinessId() > 0) {
            query.put("applyBusinessId", workflowApplyDTOQuery.getBusinessId());
        }

        if (workflowApplyDTOQuery.getBeforeNodeSort() != null) {
            query.put("beforeNodeSort", workflowApplyDTOQuery.getBeforeNodeSort());
        }

        if (workflowApplyDTOQuery.getAfterNodeSort() != null) {
            query.put("afterNodeSort", workflowApplyDTOQuery.getAfterNodeSort());
        }

        if (workflowApplyDTOQuery.getStatus() != null && workflowApplyDTOQuery.getStatus() != -9999) {
            query.put("applyStatus", workflowApplyDTOQuery.getStatus());
        }

        if (workflowApplyDTOQuery.getPassLineStatus() != null) {
            query.put("passLineStatus", workflowApplyDTOQuery.getPassLineStatus());
        }

        if (workflowApplyDTOQuery.getStopFlag() != null) {
            query.put("applyStopFlag", workflowApplyDTOQuery.getStopFlag());
        }

        if (StringUtils.isNotBlank(workflowApplyDTOQuery.getOrderBy())) {
            query.put("orderBy", workflowApplyDTOQuery.getOrderBy());
        }

        //申请日期
        if (StringUtils.isNotBlank(workflowApplyDTOQuery.getDaterange())) {
            String[] date = workflowApplyDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        //不包括取消和撤消、驳回数据（废弃）
        if (workflowApplyDTOQuery.getNoCancle() != null && workflowApplyDTOQuery.getNoCancle() == 1) {
            query.put("applyNoCancle", workflowApplyDTOQuery.getNoCancle());
        }

        //显示取消数据
        if (workflowApplyDTOQuery.getShowCancle() != null && workflowApplyDTOQuery.getShowCancle() == 1) {
            query.remove("applyStopFlag");
        }

        //显示驳回数据
        if (workflowApplyDTOQuery.getShowReject() != null && workflowApplyDTOQuery.getShowReject() == 1) {
            List<Integer> list = workflowApplyDTOQuery.getStatuList();
            list.add(-1);
            workflowApplyDTOQuery.setStatuList(list);
        }

        //显示撤回数据
        if (workflowApplyDTOQuery.getShowRecall() != null && workflowApplyDTOQuery.getShowRecall() == 1) {
            List<Integer> list = workflowApplyDTOQuery.getStatuList();
            list.add(-10);
            workflowApplyDTOQuery.setStatuList(list);
        }

        //要查询的状态
        if (workflowApplyDTOQuery.getStatuList() != null && workflowApplyDTOQuery.getStatuList().size() > 0) {
            query.put("statuList", workflowApplyDTOQuery.getStatuList());
        }

        List<WorkflowApplyDTO> list = convert(workflowApplyService.selectJoinSecondaryForPage(query));
        Page<WorkflowApplyDTO> page = Page.from(workflowApplyDTOQuery.getPn(), workflowApplyDTOQuery.getPageSize(), list);
        page.setTotalItems(workflowApplyService.countJoinSecondaryByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return workflowApplyService.deleteById(id);
        } catch (Exception e) {
            logger.error("WorkflowApply delete failed.", e);
            throw new RuntimeException("WorkflowApply delete failed, Please order your code.", e);
        }
    }
}